package com.hdhere.application.service;

import com.hdhere.application.Repository.media.VideoCategoryRepository;
import com.hdhere.domain.common.Pagination;
import com.hdhere.domain.media.QVideoCategory;
import com.hdhere.domain.media.VideoCategory;
import com.hdhere.domain.media.page.VideoCategoryPagination;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.Expressions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class VideoCategoryService extends BaseService{

    @Autowired
    private VideoCategoryRepository videoCategoryRepository;

    public Pagination getPage(VideoCategoryPagination page){
        var cate = QVideoCategory.videoCategory;
        Predicate pre = Expressions.asBoolean(true).isTrue();

        if(page.getParentId() != null)
            pre = ExpressionUtils.and(pre, cate.parentId.eq(page.getParentId()));

        Page data = videoCategoryRepository.findAll(page);
        page.setPage(data);
        return page;
    }

    public List<VideoCategory> getALL(){

        return videoCategoryRepository.findAll();
    }

    public List<VideoCategory> getByParentId(Long parentId){

        return videoCategoryRepository.findByParentId(parentId);
    }

    public VideoCategory getOne(Long id){
        return videoCategoryRepository.getOne(id);
    }

    public void save(VideoCategory cate){
        videoCategoryRepository.save(cate);
    }

    @Transactional
    public Long delete(Long[] id){
        var q = QVideoCategory.videoCategory;
        return
        queryFactory.delete(q).where(q.id.in(id))
                .execute();
    }
}
